package controller;

import entity.Skill;
import entity.Staff;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.SkillService;
import service.StaffService;
import service.StaffSkillService;
import tool.JsonResult;
import tool.Pager;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller//控制器注解
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;

    @Autowired
    private SkillService skillService;

    @Autowired
    private StaffSkillService staffSkillService;


    @RequestMapping("getstaffs")
    public String getstaffs(Staff staff, HttpSession session) {
        System.out.println("dogetstaffs");
        System.out.println(staff);


        List<Staff> staffs = staffService.getstaffs(staff);
        List<Skill> skills = skillService.getSkills();
        System.out.println(skills);

        session.setAttribute("skills", skills);

        System.out.println("当前：" + staffs);
        session.setAttribute("staffs", staffs);

        session.setAttribute("searchstaff",staff);

        return "staffindex";
    }


    @RequestMapping("doaddstaff")
    @ResponseBody
    public JsonResult doaddstaff(Staff staff, Integer[] ids) {
        System.out.println("doaddstaff");
        System.out.println(staff);


        for (Integer id : ids) {
            System.out.println(id);
        }
        Integer result = 0;

        int firststep = staffService.addstaff(staff);

        if (firststep > 0) {
            System.out.println(staff.getStaffid());
            for (Integer skillid : ids) {

                System.out.println(skillid);
                result += staffSkillService.addStaffSkill(staff.getStaffid(), skillid);
                System.out.println(result);
            }

            if (result == ids.length) {
                System.out.println("dddd");
                return new JsonResult(true, 10000, "添加成功", null);
            } else {
                return new JsonResult(false, 10001, "添加失败", null);
            }


        }

        return new JsonResult(false, 10001, "添加失败", null);


    }

    @RequestMapping("del/{staffid}")
    @ResponseBody
    public JsonResult delstaff(@PathVariable Integer staffid) {
        System.out.println("delid" + staffid);

        Integer result = staffService.delstaff(staffid);
        System.out.println(result);
        if (result > 0) {
            return new JsonResult(true, 10000, "删除成功", null);
        } else {
            return new JsonResult(false, 10001, "删除失败", null);

        }
    }

    @RequestMapping("doupdatestaff")
    @ResponseBody
    public JsonResult doupdatestaff(Integer[] ids, Staff staff) {
        System.out.println("doupdatestaff");

        System.out.println(staff);

        Integer first = staffService.updateStaff(staff);
        if (first != 1) {
            return new JsonResult(false, 10001, "修改失败1", null);
        }

        Integer staffid = staff.getStaffid();
        Staff staffupdated = staffService.getStaffById(staffid);
        System.out.println("更改后的staff"+staffupdated);

        int delnum=staffSkillService.delStaffSkill(staffid);
        System.out.println("删除返回"+delnum);


        int readd = 0;

        for (Integer readdskillid : ids) {
            System.out.println("当前加的skillid"+readdskillid);
            readd += staffSkillService.addStaffSkill(staff.getStaffid(), readdskillid);
            System.out.println(readd);
        }

        if (readd != ids.length) {
            return new JsonResult(false, 10001, "修改失败3", null);
        }


        return new JsonResult(false, 10000, "修改成功", null);
    }


    @RequestMapping("updatestaff/{staffid}")
    public String updatestaff(@PathVariable Integer staffid,HttpSession session) {
        System.out.println("updatestaff");
        System.out.println(staffid);

        Staff updatestaff = staffService.getStaffById(staffid);
        session.setAttribute("updatestaff",updatestaff);


        return "staffupdate";
    }



}
